package com.wintop.youyuan.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.wintop.youyuan.model.BaseData;
import com.wintop.youyuan.model.DetailData;
import com.wintop.youyuan.model.Luck;
import com.wintop.youyuan.model.Message;
import com.wintop.youyuan.model.PersonalsCondition;
import com.wintop.youyuan.model.User;
import com.wintop.youyuan.services.MailBoxServices;
import com.wintop.youyuan.services.UserInformationServices;
import com.wintop.youyuan.services.UserServices;
import com.wintop.youyuan.util.ActionUtil;
import com.wintop.youyuan.util.CalculateUtil;

@Scope("prototype")
@Controller
public class MailboxAction extends ActionSupport implements RequestAware,
		SessionAware, ApplicationAware, ServletResponseAware {
	private static final long serialVersionUID = 1L;
	private Map<String, Object> request;
	private Map<String, Object> session;
	private Map<String, Object> application;
	private HttpServletResponse response;
	private User user;
	private UserServices userServices;
	private UserInformationServices userInformationServices;
	private MailBoxServices mailBoxServices;
	private BaseData baseData;
	private DetailData detailData;
	private PersonalsCondition personalsCondition;
	private Message message;
	private HashMap<String, Object> otherData;// 临时数据储存

	public String redirectMyEmail() {// 我的邮箱
		otherData = new HashMap<String, Object>();
		user = (User) session.get("userInfo");

		List<Message> messages = mailBoxServices.getMessages(user.getUserid());
		List<Object> objects = new ArrayList<Object>();
		for (Message m : messages) {
			Map<String, Object> map = new HashMap<String, Object>();
			User user = userServices.getUser(m.getSendUser());
			BaseData baseData = userInformationServices.getBaseData(m
					.getSendUser());
			map.put("name", user.getName());
			map.put("icon",
					ActionUtil.getIcon(user.getUserid(), baseData.getIcon()));
			map.put("age", CalculateUtil.getAge(baseData.getBirthday()));
			map.put("apartment",
					ActionUtil.getApartment(baseData.getApartment()));
			map.put("time", CalculateUtil.getDeltaT(m.getTime()));
			map.put("num", 1);
			map.put("sendUser", m.getSendUser());
			boolean isAdd = true;
			for (Object o : objects) {
				@SuppressWarnings("unchecked")
				HashMap<String, Object> map2 = (HashMap<String, Object>) o;
				if (map2.get("sendUser").equals(m.getSendUser())) {
					map2.put("num", (int) map2.get("num") + 1);
					isAdd = false;
				}
			}
			if (isAdd) {
				objects.add(map);
			}
		}

		otherData.put("messages", objects);
		otherData.put("messageNum",
				mailBoxServices.getMessageNum(user.getUserid()));
		otherData.put("informNum",
				mailBoxServices.getInformNum(user.getUserid()));

		return "redirectMyEmail";
	}

	public String redirectAttention() {// 关注
		otherData = new HashMap<String, Object>();
		user = (User) session.get("userInfo");

		List<Luck> lucks = mailBoxServices.getLucks(user.getUserid(),
				"myAttention");
		List<Object> objects = new ArrayList<Object>();
		for (Luck l : lucks) {
			Map<String, Object> map = new HashMap<String, Object>();
			User user = userServices.getUser(l.getValue1());
			BaseData baseData = userInformationServices.getBaseData(l
					.getValue1());
			DetailData detailData = userInformationServices.getDetailData(l
					.getValue1());
			map.put("name", user.getName());
			map.put("icon",
					ActionUtil.getIcon(user.getUserid(), baseData.getIcon()));
			map.put("age", CalculateUtil.getAge(baseData.getBirthday()));
			map.put("apartment",
					ActionUtil.getApartment(baseData.getApartment()));
			map.put("sendUser", l.getValue1());
			map.put("soliloquy", detailData.getSoliloquy());
			map.put("luckId", l.getId());
			objects.add(map);
		}

		otherData.put("lucks", objects);
		otherData.put("messageNum",
				mailBoxServices.getMessageNum(user.getUserid()));
		otherData.put("informNum",
				mailBoxServices.getInformNum(user.getUserid()));
		return "redirectAttention";
	}

	public String redirectRecentContacts() {// 最近联系
		otherData = new HashMap<String, Object>();
		user = (User) session.get("userInfo");

		List<Message> messages = mailBoxServices.getRecentMessages(user
				.getUserid());
		List<Object> objects = new ArrayList<Object>();
		for (Message m : messages) {
			Map<String, Object> map = new HashMap<String, Object>();
			User user = userServices.getUser(m.getReceiverUser());
			BaseData baseData = userInformationServices.getBaseData(m
					.getReceiverUser());
			DetailData detailData = userInformationServices.getDetailData(m
					.getReceiverUser());
			map.put("name", user.getName());
			map.put("icon",
					ActionUtil.getIcon(user.getUserid(), baseData.getIcon()));
			map.put("age", CalculateUtil.getAge(baseData.getBirthday()));
			map.put("apartment",
					ActionUtil.getApartment(baseData.getApartment()));
			map.put("time", CalculateUtil.getDeltaT(m.getTime()));
			map.put("sendUser", m.getReceiverUser());
			map.put("soliloquy", detailData.getSoliloquy());
			boolean isAdd = true;
			for (Object o : objects) {
				@SuppressWarnings("unchecked")
				HashMap<String, Object> map2 = (HashMap<String, Object>) o;
				if (map2.get("sendUser").equals(m.getReceiverUser())) {
					isAdd = false;
				}
			}
			if (isAdd) {
				objects.add(map);
			}
		}

		otherData.put("messages", objects);
		otherData.put("messageNum",
				mailBoxServices.getMessageNum(user.getUserid()));
		otherData.put("informNum",
				mailBoxServices.getInformNum(user.getUserid()));
		return "redirectRecentContacts";
	}

	public String redirectInform() {// 通知
		return "redirectInform";
	}

	public String redirectReadMessage() {// 阅读消息
		otherData = new HashMap<String, Object>();
		if (user.getId() == 0) {
			otherData.put("readType", "mailbox_redirectMyEmail");
		} else if (user.getId() == 1) {
			otherData.put("readType", "mailbox_redirectAttention");
		} else if (user.getId() == 2) {
			otherData.put("readType", "mailbox_redirectRecentContacts");
		}
		user = (User) session.get("userInfo");
		List<Message> messages = mailBoxServices.getMessages(user.getUserid(),
				message.getSendUser());
		User sendUser = userServices.getUser(message.getSendUser());
		mailBoxServices.readMessage(user.getUserid(), message.getSendUser());// 标记消息已读
		BaseData sendBaseData = userInformationServices.getBaseData(sendUser
				.getUserid());
		otherData.put("sendIcon", ActionUtil.getIcon(sendUser.getUserid(),
				sendBaseData.getIcon()));
		otherData.put("sendName", sendUser.getName());
		otherData.put("sendId", sendUser.getId());
		otherData.put("sendUserId", sendUser.getUserid());
		otherData.put("sex", ActionUtil.getSex(sendUser.getSex()));
		otherData.put("age", CalculateUtil.getAge(sendBaseData.getBirthday()));
		otherData.put("apartment",
				ActionUtil.getApartment(sendBaseData.getApartment()));
		otherData.put("marryCondition",
				ActionUtil.getMarryCondition(sendBaseData.getMarryCondition()));
		otherData.put("education",
				ActionUtil.getEducation(sendBaseData.getEducation()));
		otherData.put("messages", messages);

		return "redirectReadMessage";
	}

	public Map<String, Object> getRequest() {
		return request;
	}

	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public Map<String, Object> getApplication() {
		return application;
	}

	public void setApplication(Map<String, Object> application) {
		this.application = application;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public BaseData getBaseData() {
		return baseData;
	}

	public void setBaseData(BaseData baseData) {
		this.baseData = baseData;
	}

	public DetailData getDetailData() {
		return detailData;
	}

	public void setDetailData(DetailData detailData) {
		this.detailData = detailData;
	}

	public PersonalsCondition getPersonalsCondition() {
		return personalsCondition;
	}

	public void setPersonalsCondition(PersonalsCondition personalsCondition) {
		this.personalsCondition = personalsCondition;
	}

	public Message getMessage() {
		return message;
	}

	public void setMessage(Message message) {
		this.message = message;
	}

	public HashMap<String, Object> getOtherData() {
		return otherData;
	}

	public void setOtherData(HashMap<String, Object> otherData) {
		this.otherData = otherData;
	}

	@Resource(name = "userServices")
	public void setUserServices(UserServices userServices) {
		this.userServices = userServices;
	}

	@Resource(name = "userInformationServices")
	public void setUserInformationServices(
			UserInformationServices userInformationServices) {
		this.userInformationServices = userInformationServices;
	}

	@Resource(name = "mailBoxServices")
	public void setMailBoxServices(MailBoxServices mailBoxServices) {
		this.mailBoxServices = mailBoxServices;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

}
